<?php

namespace app\api\controller;

use app\common\controller\Base;
use app\common\model\AddressArea;
use app\common\model\Center4s;
use app\common\model\InsuranceCompany;
use app\common\model\Parts;
use app\common\model\PartsArt;
use app\common\model\ReturnType;
use app\common\model\Tasks;
use app\common\model\User;
use app\common\model\UserGroup;
use PhpOffice\PhpSpreadsheet\Calculation\Database\DProduct;
use think\Db;
use think\Queue;

/**
 * 任务接口
 */
class Task extends Base
{
    protected $noNeedLogin = ['center_4s'];
    protected $noNeedRight = ['hasCreate','pre','center_4s','index','detail','take','refuse','refuse_cancel','pick','deliver','service_start','service_cancel','service_details','service','return','settles','settlement','refuse_oper','parts_art'];

    /*
     * 初始化
     */
    public function pre()
    {
        $data['pick_user'] = User::getPickUser();
        $data['return_type'] = ReturnType::getList();
        $data['center_4s'] = Center4s::getList();
        $data['insurance_company'] = InsuranceCompany::getList();
        $data['parts'] = Parts::getList();
        $data['address_area'] = AddressArea::getAreaList();
        $this->success('初始化',$data);
    }

    /**
     * 获取4s店
     */
    public function center_4s()
    {
        $keywords = $this->request->get("keywords");
        $this->success('4s店搜索',Center4s::getSearchLists($keywords));
    }

    /*
     * 判断是否有发布的权限
     */
    public function hasCreate()
    {
        $my = $this->auth->getRuleList();
        $my_rules = array_column($my,'name');
        $flag = false;
        if (in_array('api/task/create',$my_rules)){
            $flag = true;
        }
        $this->success('权限判断',$flag);
    }


    /*
     * 创建任务
     */
    public function create()
    {
        repeat_filter();        // 防抖

        $params = $this->request->post();
        //参数校验
        $this->apiValidate($params, \app\api\validate\Task::class,'edit');
        //id不存在执行添加
        if (!isset($params['id']) || !$params['id']){
            $data = Tasks::operCreate($params);
            $this->success('任务发布成功',$data);
        }
        //如果任务不存在也执行添加
        $task = Tasks::get($params['id']);
        if (!$task){
            $data = Tasks::operCreate($params);
            $this->success('任务发布成功',$data);
        }
        //已经完成的任务编辑重新发布
        if ($task['status'] == Tasks::STATUS_FINISH) {
            $data = Tasks::operCreate($params);
            $this->success('操作成功',$data);
        }

        //只能编辑接单失败的任务 任务状态:-3=已拒绝接单-2=拒绝接单中,-1=接单失败,0=待接单,1=已接单,待取件，2=已取件,3=取件已送达,待维修,4=维修中,5=维修完成,还件中,6=任务已完成
        $arr = [Tasks::STATUS_WAIT,Tasks::STATUS_INVALID,Tasks::STATUS_REFUSED];
        if (!in_array($task['status'],$arr)){
            $this->error('任务进行中，不可编辑');
        }
        //执行编辑操作
        $data = Tasks::operEdit($params);
        $this->success('任务发布成功',$data);
    }

    /*
     * 任务列表
     */
    public function index()
    {
        $params = $this->request->get();
        $data = [];
        //用户的角色数组
        $group_ids = explode(',',$this->auth->group_ids);
        //如果是超级管理员，可以看到全部订单
        if (in_array(UserGroup::GROUP_ADMIN,$group_ids)){
            $data['count'] = Tasks::getTypeCount('all');
            $data['data'] = Tasks::getLists($params,'all');
            $this->success('任务列表',$data);
        }
        //如果用户是维修人，只能看到自己维修下的订单统计
        if (in_array(UserGroup::GROUP_SERVICE,$group_ids)){
//            $data['count'] = Tasks::getTypeCount('service');
//            $data['data'] = Tasks::getLists($params,'service');
            //后面说维修人能看到所有列表
            $data['count'] = Tasks::getTypeCount('all');
            $data['data'] = Tasks::getLists($params,'all');
            $this->success('任务列表',$data);
        }
        //如果是取件人，只能看到当前取件人是自己的订单信息
        if (in_array(UserGroup::GROUP_PICK,$group_ids)){
            $data['count'] = Tasks::getTypeCount('pick');
            $data['data'] = Tasks::getLists($params,'pick');
            $this->success('任务列表',$data);
        }
        //如果是其它人，只能看到待还件列表
        $data['count'] = Tasks::getTypeCount('other');
        $data['data'] = Tasks::getLists($params,'other');
        $this->success('任务列表',$data);
    }

    /*
     * 任务单详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('详情',Tasks::getDetail($id));
    }

    /*
     * 确认接单
     */
    public function take()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('确认接单',Tasks::operTake($id));
    }

    /*
     * 拒绝接单
     */
    public function refuse()
    {
        $id = $this->request->post('id');
        $remark = $this->request->post('remark');
        if (!$id || !$remark) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('拒绝接单',Tasks::operRefuse($id,$remark));
    }

    /*
     * 取消任务拒接
     */
    public function refuse_cancel()
    {
        $id = $this->request->post('id');
        $this->success('拒接已取消',Tasks::operRefuseCancel($id));
    }

    /*
     * 拒接审核
     */
    public function refuse_oper()
    {
        $id = $this->request->post('id');
        $oper = $this->request->post('oper');
        if (!$id || !$oper) {
            $this->error(__('Invalid parameters'));
        }
        $task = Tasks::where(['id' => $id])->find();
        if (!$task) {
            $this->error('任务不存在');
        }
        if ($task->status != -2) {
            $this->error('该任务或已经被审核');
        }
        //如果当前用户是发布人,可以操作
        if ($this->auth->id == $task->user_id){
            $this->success('操作成功',Tasks::operRefuseStatus($task,$this->auth->getUser(),$oper));
        }
        //用户的角色ids数组
        $group_ids = array_column($this->auth->groups,'id');
        //如果用户是超级管理员，可以操作
        if (in_array(UserGroup::GROUP_ADMIN,$group_ids)){
            $this->success('操作成功',Tasks::operRefuseStatus($task,$this->auth->getUser(),$oper));
        }
        //如果用户是取件人或维修人的身份，禁止操作
        if (in_array(UserGroup::GROUP_PICK,$group_ids) || in_array(UserGroup::GROUP_SERVICE,$group_ids)){
            $this->error('您没有权限操作');
        }
    }

    /*
     * 确认取件
     */
    public function pick()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'pick');
        $this->success('确认取件',Tasks::operPick($params));
    }

    /*
     * 确认送达
     */
    public function deliver()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'deliver');
        $this->success('确认送达',Tasks::operDeliver($params));
    }

    /*
     * 开始维修
     */
    public function service_start()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'service_start');
        $this->success('开始维修',Tasks::operServiceStart($params));
    }

    /*
     * 无法维修
     */
    public function service_cancel()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'service_cancel');
        $this->success('无法维修',Tasks::operServiceCancel($params));
    }

    /*
     * 维修明细
     */
    public function service_details()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\TaskService::class,'service_details');
        $this->success('操作成功',Tasks::OperServiceDetails($params));
    }

    /*
     * 维修完成
     */
    public function service()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'service');
        $this->success('维修完成',Tasks::operService($params));
    }

    /*
     * 确认还件
     */
    public function return()
    {
        $params = $this->request->post();
        $this->apivalidate($params, \app\api\validate\Task::class,'return');
        $this->success('确认还件',Tasks::operReturn($params));
    }

    /*
     * 配件工艺
     */
    public function parts_art()
    {
        $task_id = $this->request->get('task_id');
        if (!$task_id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('配件工艺',PartsArt::getTaskPartLists($task_id));
    }

    /*
     * 结算查询
     */
    public function settles()
    {
        $params = $this->request->get();
        $this->success('结算列表',Tasks::getSettleLists($params));
    }

    /*
     * 任务结算
     */
    public function settlement()
    {
        $params = $this->request->post();
        $this->apiValidate($params, \app\api\validate\Task::class,'settlement');
        $this->success('确认结算',Tasks::operSettlement($params));
    }

}
